home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 12 - 1996 / 12.02 Feb 96 / Adding Scripts to Menus / Script Menu test project / Powering Up Code / SCModelProperty.cp < prev    next >
Encoding:
Text File  |  1995-06-15  |  5.4 KB  |  210 lines  |  [TEXT/MMCC]

  1. // ===========================================================================
  2. // SCModelProperty -- a model property that automatically handles undo
  3. // ===========================================================================
  4. // © 1994 James Kaput, SimCalc Project
  5.  
  6. #include "SCModelProperty.h"
  7. #include "SCApp.h"
  8.  
  9. SCModelProperty::SCModelProperty(DescType                inPropertyID,
  10.                                  LModelObject            *inSuperModel) :
  11.         LModelProperty(inPropertyID,nil) // nil prevents AddSubModel call
  12. {
  13.     mSuperModel = inSuperModel;
  14. }
  15.  
  16. SCModelProperty::~SCModelProperty()
  17. {
  18.     mSuperModel = nil;                     // nil prevents RemoveSubModel call
  19. }
  20.                         
  21. void    
  22. SCModelProperty::HandleSetData(const AppleEvent    &inAppleEvent,
  23.                             AppleEvent            &outAEReply)
  24. {
  25.  
  26.     SCApp::PostAction(new SCSetPropertyAction(GetSuperModel(),mPropertyID,inAppleEvent));
  27. }
  28.  
  29. LDynamicArray    *SCSetPropertyAction::sPropDescArray = nil;
  30.  
  31. SCSetPropertyAction::SCSetPropertyAction(LModelObject    *inObject,
  32.                                 DescType    inPropID,
  33.                                 const AppleEvent        &inAE)
  34.     :  LAction(200,3),   mObject(inObject), mPropID(inPropID)
  35.     
  36. {
  37.     // get old data from object itself
  38.     StAEDescriptor    reply;
  39.     mObject->GetAEProperty(mPropID, reply.mDesc, mOldData.mDesc);
  40.     
  41.     mNewData.GetParamDesc(inAE, keyAEData, typeWildCard);
  42.         
  43.     UAppleEventsMgr::CheckForMissedParams(inAE);
  44. }
  45.  
  46. SCSetPropertyAction::~SCSetPropertyAction()
  47. {
  48.     // descriptors automatically deleted!
  49. }
  50.  
  51.  
  52. void 
  53. SCSetPropertyAction::RedoSelf()
  54. {
  55.     StAEDescriptor    result;
  56.     mObject->SetAEProperty(mPropID,mNewData.mDesc,result.mDesc);
  57. }
  58.  
  59. void 
  60. SCSetPropertyAction::UndoSelf()
  61. {
  62.     StAEDescriptor    result;
  63.     mObject->SetAEProperty(mPropID,mOldData.mDesc,result.mDesc);
  64. }
  65.  
  66. void 
  67. SCSetPropertyAction::GetDescriptor(Str63 outDescription)
  68. {
  69.     DescribeProperty(mPropID,outDescription);
  70. }
  71.  
  72.  
  73. // this parses the 'aete' resource to build an array of associations
  74. // between 4-char property ids and Str255 property names        
  75. void
  76. SCSetPropertyAction::InitPropDescriptors()
  77. {
  78.     Handle            terminology = Get1Resource('aete',0);
  79.     LHandleStream    stream(terminology);
  80.     char            buffer[20];
  81.     Str255            pstring;
  82.     short            suiteCount, eventCount, classCount, compCount, enumCount,
  83.                     propCount, paramCount, tagCount, elemCount, formCount;
  84.     SPropDesc        prop;
  85.     
  86.     sPropDescArray = new LDynamicArray(sizeof(SPropDesc));
  87.     
  88.     stream.ReadData(buffer,6);         // skip preliminaries
  89.     stream.ReadData(&suiteCount,2); // count of suites
  90.     
  91.     while(suiteCount > 0) {
  92.         stream.ReadPString(pstring);        // suite name
  93.         stream.ReadPString(pstring);        // suite description
  94.         ALIGN_WORD
  95.         stream.ReadData(buffer,8);             // skip suite stuff
  96.         
  97.         stream.ReadData(&eventCount,2); // count of events
  98.         while (eventCount > 0) {
  99.             stream.ReadPString(pstring);        // event name
  100.             stream.ReadPString(pstring);        // event description
  101.             ALIGN_WORD
  102.             stream.ReadData(buffer,12);         // skip event stuff
  103.             stream.ReadPString(pstring);        // reply description
  104.             ALIGN_WORD
  105.             stream.ReadData(buffer,6);             // skip reply stuff
  106.             stream.ReadPString(pstring);        // direct object description
  107.             ALIGN_WORD
  108.             stream.ReadData(buffer,2);             // skip direct object stuff
  109.             
  110.             stream.ReadData(¶mCount,2); // count of parameters
  111.             while(paramCount > 0) {
  112.                 stream.ReadPString(pstring);        // param name
  113.                 ALIGN_WORD
  114.                 stream.ReadData(buffer,8);             // skip param stuff
  115.                 stream.ReadPString(pstring);        // param description
  116.                 ALIGN_WORD
  117.                 stream.ReadData(buffer,2);             // skip param stuff
  118.                 paramCount--;
  119.             }
  120.             eventCount--;
  121.         }
  122.         
  123.         stream.ReadData(&classCount,2);
  124.         while (classCount > 0) {
  125.             stream.ReadPString(pstring);        // class name
  126.             ALIGN_WORD
  127.             stream.ReadData(buffer,4);             // skip class stuff
  128.             stream.ReadPString(pstring);        // class desc
  129.             ALIGN_WORD
  130.             
  131.             stream.ReadData(&propCount,2);         // count of properties
  132.             
  133.             while (propCount > 0) {
  134.                 stream.ReadPString(prop.name);     // get prop name
  135.                 ALIGN_WORD
  136.                 stream.ReadData(&(prop.id),4);         // get prop id
  137.                 stream.ReadData(buffer,4);             // skip rest of prop
  138.                 stream.ReadPString(pstring);         // get prop desc
  139.                 ALIGN_WORD
  140.                 stream.ReadData(buffer,2);             // skip rest of prop
  141.                 
  142.                 sPropDescArray->InsertItemsAt(1,arrayIndex_Last,&prop);
  143.             
  144.                 propCount--;
  145.             }
  146.             
  147.             stream.ReadData(&elemCount,2);         // count of elements
  148.             
  149.             while (elemCount > 0) {
  150.                 stream.ReadData(buffer,4);                     // skip elem id
  151.                 stream.ReadData(&formCount,2);                 // skip elem id
  152.                 stream.ReadData(buffer,formCount * 4);         // skip forms
  153.             
  154.             
  155.                 elemCount--;
  156.             }
  157.  
  158.         
  159.             classCount--;
  160.         }
  161.         
  162.         stream.ReadData(&compCount,2);
  163.         while(compCount > 0) {
  164.             stream.ReadPString(pstring);         //  comp name
  165.             ALIGN_WORD
  166.             stream.ReadData(buffer,4);             // skip rest of prop
  167.             stream.ReadPString(pstring);         //  comp desc
  168.             ALIGN_WORD
  169.             compCount--;
  170.         }
  171.  
  172.         stream.ReadData(&enumCount,2);
  173.         while(enumCount > 0) {
  174.             stream.ReadData(buffer,4);             // skip enumerator
  175.             stream.ReadData(&tagCount,2);
  176.             while (tagCount > 0) {
  177.                 stream.ReadPString(pstring);         //  tag name
  178.                 ALIGN_WORD
  179.                 stream.ReadData(buffer,4);             // skip rest of tag
  180.                 stream.ReadPString(pstring);         //  tag desc
  181.                 ALIGN_WORD
  182.                 tagCount--;
  183.             }
  184.             enumCount--;
  185.         }    
  186.         suiteCount--;
  187.     }
  188. }
  189.  
  190. // Jeremy 012 Mod
  191. Boolean        
  192. SCSetPropertyAction::DescribeProperty(DescType inPropID, Str63 outDescriptor)
  193. {
  194.     if (! sPropDescArray) InitPropDescriptors();
  195.     
  196.     SPropDesc    prop;
  197.     long        i,count = sPropDescArray->GetCount();
  198.     
  199.     for(i = 1; i <= count; i++) {
  200.         sPropDescArray->FetchItemAt(i,&prop);
  201.         if (prop.id == inPropID) {
  202.             CopyPStr(prop.name,outDescriptor);
  203.             return true;
  204.         }
  205.     }
  206.     return false;
  207. }
  208.  
  209.  
  210.